package com.ptu.controller.admin;

import com.ptu.dto.SeckillProductDTO;
import com.ptu.entity.SeckillProduct;
import com.ptu.server.OSSService;
import com.ptu.server.SeckillProductServer;
import com.ptu.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController("adminSeckillProductController")
@RequestMapping("/admin/seckillProduct")
@Tag(name = "秒杀商品模块")
public class SeckillProductController {

    @Autowired
    private SeckillProductServer seckillProductServer;

    @Autowired
    private OSSService ossService;


    @Operation(summary = "添加秒杀商品")
    @PostMapping("/add")
    public Result<SeckillProduct> addSeckillProduct(@RequestBody SeckillProductDTO seckillProductDTO) {
        System.out.println("添加秒杀商品");
        seckillProductServer.addSeckillProduct(seckillProductDTO);
        return Result.success();
    }

    @Operation(summary = "获取秒杀商品列表")
    @PostMapping("/list")
    public List<SeckillProduct> list() {
        System.out.println("获取秒杀商品列表");
        List<SeckillProduct> list = seckillProductServer.list();
        return list;
    }

    @Operation(summary = "删除秒杀商品")
    @PostMapping("/delete")
    public Result<Void> delete(Long id) {
        System.out.println("删除秒杀商品");
        seckillProductServer.delete(id);
        return Result.success();
    }

    @Operation(summary = "修改秒杀商品")
    @PostMapping("/update")
    public Result<Void> update(@RequestBody SeckillProductDTO seckillProductDTO) {
        System.out.println("修改秒杀商品");
        seckillProductServer.update(seckillProductDTO);
        return Result.success();
    }

    @Operation(summary = "获取秒杀商品详情")
    @GetMapping("/detail/{id}")
    public Result<SeckillProduct> detail(@PathVariable Long id) {
        System.out.println("获取秒杀商品详情");
        return Result.success(seckillProductServer.detail(id));
    }

    @Operation(summary = "管理员上架、下架秒杀商品")
    @PostMapping("/status")
    public Result<Void> status(@RequestParam Long id) {
        System.out.println("管理员上架、下架秒杀商品");
        seckillProductServer.status(id);
        return Result.success();
    }

    @Operation(summary = "管理员上传秒杀商品图片")
    @PostMapping("/upload/seckillProduct")
    public Result<List<String>> uploadProductImages(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam(value = "id", required = false) Long id) {

        System.out.println("上传秒杀商品图片");

        // 验证文件数量
        if (files == null || files.length == 0) {
            return Result.error("请上传至少一张图片");
        }
        if (files.length > 1) { // 限制最多5张图片
            return Result.error("最多上传1张图片");
        }

        List<String> imageUrls = new ArrayList<>();

        try {
            for (MultipartFile file : files) {
                // 验证文件类型
                String contentType = file.getContentType();
                if (contentType == null || !contentType.startsWith("image/")) {
                    return Result.error("请上传图片文件");
                }


                // 上传到OSS，使用不同的文件夹区分商品图片
                String imageUrl = ossService.uploadProductImage(file, "SeckillProducts/");
                imageUrls.add(imageUrl);
            }

            // 如果有productId，可以在这里更新商品图片关联
            if (id != null) {
                // 调用商品服务更新图片
                // productService.updateProductImages(productId, imageUrls);
            }

            return Result.success(imageUrls);
        } catch (IOException e) {
            System.out.println("上传失败" + e);
            return Result.error("上传失败");
        }
    }



}
